package com.six.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.bean.Customer;
import com.six.service.CustomerService;
import com.six.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
    @RestController
    @RequestMapping("customer")
    @CrossOrigin
    public class CustomerController {
        @Autowired
        CustomerService customerService;
        @GetMapping("list")
        public Result getList(){
            List<Customer> list = customerService.list();
            return Result.success(list);
        }
        @GetMapping("page")
        public Result getPage(Page page){
            IPage result = customerService.page(page);
            return Result.success(result);
        }
        @PostMapping
        public Result add(@RequestBody Customer customer){
            customerService.save(customer);
            Customer customer1=customerService.getById(customer.getCid());
            return Result.success("保存成功",customer1);
        }
        @DeleteMapping("/{cid}")
        public Result delete(@PathVariable("cid") int cid){
            boolean b=customerService.removeById(cid);
            if(b){
                return Result.success("删除成功");
            }
            else{
                return Result.fail(201,"删除失败");
            }
        }
        @PutMapping
        public Result edit(@RequestBody Customer customer){
            boolean b = customerService.saveOrUpdate(customer);
            Customer customer1=customerService.getById(customer.getCid());
            if(b){
                return Result.success("保存成功",customer1);
            }else {
                return Result.fail(201,"修改失败");
            }
        }
    }

